package org.tinygroup.bizframe.business.impl;

import org.tinygroup.bizframe.business.inter.SysBranchBusiness;
import org.tinygroup.bizframe.dao.inter.TsysBranchDao;
import org.tinygroup.bizframe.dao.inter.TsysBranchUserDao;
import org.tinygroup.bizframe.dao.inter.TsysUserDao;
import org.tinygroup.bizframe.dao.inter.pojo.TreeData;
import org.tinygroup.bizframe.dao.inter.pojo.TsysBranch;
import org.tinygroup.bizframe.dao.inter.pojo.TsysBranchUser;
import org.tinygroup.bizframe.dao.inter.pojo.TsysUser;
import org.tinygroup.jdbctemplatedslsession.daosupport.OrderBy;
import org.tinygroup.tinysqldsl.Pager;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Mr.wang on 2016/7/14.
 */
public class SysBranchBusinessImpl implements SysBranchBusiness {
	private TsysBranchDao tsysBranchDao;

	private TsysUserDao tsysUserDao;
	
	private TsysBranchUserDao tsysBranchUserDao;

	public TsysBranchDao getTsysBranchDao() {
		return tsysBranchDao;
	}

	public void setTsysBranchDao(TsysBranchDao tsysBranchDao) {
		this.tsysBranchDao = tsysBranchDao;
	}

	public TsysBranch getById(String branchCode) {
		return tsysBranchDao.getByKey(branchCode);
	}

	public int deleteByKeys(String... pks) {
		return tsysBranchDao.deleteByKeys(pks);
	}

	public Pager<TsysBranch> getPager(int start, int limit, TsysBranch tsysBranch, OrderBy... orderBies) {
		return tsysBranchDao.queryFuzzyPager(start, limit, tsysBranch, orderBies);
	}

	public TsysBranch add(TsysBranch tsysBranch) {
		return tsysBranchDao.add(tsysBranch);
	}

	public int update(TsysBranch tsysBranch) {
		return tsysBranchDao.edit(tsysBranch);
	}

	public boolean checkExists(TsysBranch tsysBranch) {
		return tsysBranchDao.checkExist(tsysBranch).size() > 0 ? true : false;
	}

	public List<TsysBranch> getList(TsysBranch tsysBranch) {
		return tsysBranchDao.query(tsysBranch);
	}

	public List getTreeData(TsysBranch tree) {
		return tsysBranchDao.getTreeData(tree);
	}

	public Pager<TsysUser> getUserPager(int start, int limit, TsysBranchUser tsysBranchUser, OrderBy... orderBies) {
		
		Pager<TsysBranchUser> pager = tsysBranchUserDao.queryPager(start, limit, tsysBranchUser, orderBies);
		List<TsysUser> records = new ArrayList<TsysUser>();
		Iterator<TsysBranchUser> itr = pager.getRecords().iterator();
        while(itr.hasNext()){
        	TsysBranchUser branchUser = itr.next();
        	TsysUser user=tsysUserDao.getByKey(branchUser.getUserId());
        	if(user!=null){
        		records.add(user);     	
        	}
        }
		Pager<TsysUser> userPager = new Pager<TsysUser>(pager.getTotalCount(), start, records);
		return userPager;
	}

	public int addBranchUser(TsysBranchUser user) {

		// 先删除，再添加
		List<TsysBranchUser> users = new ArrayList<TsysBranchUser>();
		users.add(user);
		tsysBranchUserDao.preparedBatchDelete(users);

		TsysBranchUser branchUser = tsysBranchUserDao.add(user);
		return branchUser.getId();
	}

	public int removeBranchUser(TsysBranchUser user) {
		List<TsysBranchUser> users = new ArrayList<TsysBranchUser>();
		users.add(user);
		int[] rets = tsysBranchUserDao.preparedBatchDelete(users);
		return rets.length;
	}

	public TsysBranchUserDao getTsysBranchUserDao() {
		return tsysBranchUserDao;
	}

	public void setTsysBranchUserDao(TsysBranchUserDao tsysBranchUserDao) {
		this.tsysBranchUserDao = tsysBranchUserDao;
	}

	public TsysUserDao getTsysUserDao() {
		return tsysUserDao;
	}

	public void setTsysUserDao(TsysUserDao tsysUserDao) {
		this.tsysUserDao = tsysUserDao;
	}
	
	

	/*
	 * public boolean checkSubBranchExist(TsysBranch sysBranch) { return
	 * tsysBranchDao.getSubBranchs(sysBranch).size()>0? true:false; }
	 */
}
